package cn.hnu.qq.client.controller;

import java.util.Set;
import java.util.Vector;

import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.CrowdInfoMessage;
import cn.hnu.qq.entity.message.IpInfoMessage;
import cn.hnu.qq.entity.message.LogonSuccessMessage;
import cn.hnu.qq.entity.message.Message;
import cn.hnu.qq.entity.message.OnlineMessage;

/**
 * 处理监听服务器消息的线程的控制器
 * @author jared
 *
 */
public class ListenServerMessageThread extends CenterController implements Runnable{
	
	private static ListenServerMessageThread instance = null;
	
	private ListenServerMessageThread(){};
	
	private Vector<MessageListener> listeners = new Vector<MessageListener>();
	
	public static synchronized ListenServerMessageThread getInstance(){
		if(instance == null){
			instance = new ListenServerMessageThread();
		}
		return instance;
	}

	public void run() {
		System.out.println("进入 ListenServerMessageThread run");
		while(true){
			try {
				Message message = (Message) getInput().readObject();
				
				log.debug("获得服务器消息 ： "+message.getMessageType());
		
				switch(message.getMessageType()){
//					case LOGON_SUCCESS:
//						handleLogonSuccess((LogonSuccessMessage) message);
//						break;
//					case IP_INFO:
//					case USER_NOT_ONLINE:
//						handleIpInfo((IpInfoMessage)message);
//						break;
					case ONLINE:
						handleOnline((OnlineMessage) message);
						break;
					default:
						handleByListener(message);
						break;
				}
			} catch (Exception e) {
				System.err.println("读取对象失败");
				e.printStackTrace();
			}
		}
	}

	private synchronized void  handleByListener(Message message) {
		log.debug("处理服务器消息 :　"+ message);
		MessageEvent event = new MessageEvent(message);
		log.debug("监听器      ： " + listeners);
		/*
		 * 不适用for each的原因是解决在迭代的同时修改监听器表
		 */
		int i = listeners.size() - 1;
		for(;i>=0;i--){
			listeners.get(i).messageArrived(event);
		}
		
	}

	private void handleOnline(OnlineMessage message) {
		Set<User> users = getCurUser().getFriends();
		for(User u:users){
			if(u.getAcount().equals(message.getAccount())){
				u.setIp(message.getIpAddr());
				u.setUserClientPort(message.getPort());
				break;
			}
		}
	}
//
//	private void handleCrowdInfo(CrowdInfoMessage message) {
//		put(message.getMessageType(),message);
//		messageArrived(message.getMessageType());
//	}

//	private void handleIpInfo(IpInfoMessage message) {
//		put(message.getMessageType(),message);
//		messageArrived(message.getMessageType());
//	}

//	private void handleLogonSuccess(LogonSuccessMessage message) {
//		setCurUser(message.getUser());
//		messageArrived(message.getMessageType());
//	}
	public synchronized void addMessageListener(MessageListener ml){
		for(MessageListener mm : listeners){
			if(mm.equals(ml)){
				return;
			}
		}
		listeners.add(ml);
	}
	
	public synchronized void removeMessageListener(MessageListener ml){
		listeners.remove(ml);
	}
	
	public synchronized void removeAllListener(){
		listeners.removeAllElements();
	}
}
